package edu.pku.sei.act.capabilityprovider;

import edu.pku.sei.act.actools.ICapabilityManager;
import edu.pku.sei.act.actools.ICapabilityProvider;
import edu.pku.sei.act.actools.IEnvEntry;
import edu.pku.sei.act.actools.util.ACToolsNamingSpace;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

public class CapabilityProviderService extends Service {

	private static ICapabilityManager cm;
	private static IEnvEntry ee;

	private static boolean START_DAEMON_STARTED = false;
	private static boolean STOP_DAEMON_STARTED = false;

	private ServiceConnection CMConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName arg0, IBinder arg1) {

			cm = ICapabilityManager.Stub.asInterface(arg1);
		}

		public void onServiceDisconnected(ComponentName name) {

			cm = null;
		}
	};
	
	private ServiceConnection EEConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName arg0, IBinder arg1) {

			ee = IEnvEntry.Stub.asInterface(arg1);
		}

		public void onServiceDisconnected(ComponentName name) {

			ee = null;
		}
	};

	@Override
	public void onCreate() {
		
		super.onCreate();
		final Intent serviceIntent1 = new Intent();
		serviceIntent1
				.setAction(ACToolsNamingSpace.ACToolsComponent.CAPABILITY_MANAGER);
		bindService(serviceIntent1, CMConnection, Context.BIND_AUTO_CREATE);

		final Intent serviceIntent2 = new Intent();
		serviceIntent2.setAction(ACToolsNamingSpace.ACToolsComponent.ENV_ENTRY);
		bindService(serviceIntent2, EEConnection, Context.BIND_AUTO_CREATE);

	}

	@Override
	public void onDestroy() {
		unbindService(CMConnection);
		unbindService(EEConnection);
		super.onDestroy();
	}
	private final ICapabilityProvider.Stub mbinder = new ICapabilityProvider.Stub() {

		public String invoke(String abilityName, String input)
				throws RemoteException {

			if (abilityName.equals(_Capabilities.START.getAbilityName())) {
				if (!START_DAEMON_STARTED) {
					new Thread(new Runnable() {

						public void run() {
							while (true) {
								try {
									cm.abilityCallback(_Capabilities.START_OUTPUT
											.toString());
									Thread.sleep(20000);
								} catch (InterruptedException e) {
									e.printStackTrace();
								} catch (RemoteException e) {
									e.printStackTrace();
								}
							}
						}
					}).start();
					START_DAEMON_STARTED = true;
				}
				return _Capabilities.START_OUTPUT.toString();
			} else if (abilityName.equals(_Capabilities.STOP.getAbilityName())) {
				if (!STOP_DAEMON_STARTED) {
					new Thread(new Runnable() {

						public void run() {
							try {
								Thread.sleep(15000);
							} catch (InterruptedException e1) {
								
								e1.printStackTrace();
							}
							while (true) {
								try {
									cm.abilityCallback(_Capabilities.STOP_OUTPUT
											.toString());
									Thread.sleep(20000);
								} catch (InterruptedException e) {
									
									e.printStackTrace();
								} catch (RemoteException e) {
									
									e.printStackTrace();
								}
							}
						}
					}).start();
					STOP_DAEMON_STARTED = true;
				}
				return _Capabilities.STOP_OUTPUT.toString();
			} else if (abilityName.equals(_Capabilities.GET_LOCATION.getAbilityName())) {
				return "I AM HERE!!";
			} else if (abilityName.equals(_Capabilities.MARK_POSITION.getAbilityName())) {
				return null;
			}
			return null;
		}
	};

	@Override
	public IBinder onBind(Intent intent) {
		return mbinder;
	}

}
